package com.schooleducation.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysql.fabric.xmlrpc.base.Data;
import com.schooleducation.entity.ExamDictinoary;
import com.schooleducation.entity.Exams;
import com.schooleducation.entity.User;
import com.schooleducation.service.ServiceFactory;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/lessonTest")
public class LessonTestController {

	@Resource(name = "serviceFac")
	private ServiceFactory serviceFac;
	/**
	 * 获取页面
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	private String getPage(HttpSession session, Model model) {
		User user=(User)session.getAttribute("user");
		if (user==null) {
			return "redirect:login";
		}
		List<Exams> exams=serviceFac.getExamsService().getUserExams(user);
		model.addAttribute("exams", exams);
		return "testList";
	}
	
	@RequestMapping(value="doTest",method = RequestMethod.GET)
	private String doTest(HttpSession session, Model model,int id) {
		Exams exams=serviceFac.getExamsService().getExam(id);
		session.setAttribute("examid", id);
		session.setAttribute("answer", exams.getAnswer());
		session.setAttribute("beginTime", new Date());
		session.setAttribute("time", exams.getTime());
		List<ExamDictinoary> examDictinoary=serviceFac.getExamsService().getTestPaper(exams);
		model.addAttribute("exam",exams);
		model.addAttribute("examDictinoaries",examDictinoary);
		Map<Integer, String> map=new HashMap<>();
		map.put(0, "A");
		map.put(1, "B");
		map.put(2, "C");
		map.put(3, "D");
		model.addAttribute("map",map);
		return "lessonTest";
	}
	
	@ResponseBody
	@RequestMapping(value="upAnswer",method = RequestMethod.POST)
	private String answer(HttpSession session,String answers) {
		//服务端计算时间，防止客户端时间被修改
		Date enDate=new Date();
		Date bgDate=(Date)session.getAttribute("beginTime");
		long temp = enDate.getTime()-bgDate.getTime();
	    long temp2 = temp % (1000 * 3600);
	    long mins = temp2 / 1000 / 60;
//	    System.out.println(mins+"**********");
	    Map<String, Object> map=new HashMap<>();
	    if (mins<=(int)session.getAttribute("time")) {
	    	String[] answer=session.getAttribute("answer").toString().split(",");
			String[] userAnswer=answers.split(",");
			int score=0;
			for (int i = 0; i < answer.length; i++) {
				if (answer[i].equals(userAnswer[i])) {
					score+=5;
				}
			}
			System.out.println(session.getAttribute("examid").toString()+String.valueOf(score));
			serviceFac.getExamsService().addScore(
					(User)session.getAttribute("user"),
					String.valueOf(score),
					session.getAttribute("examid").toString());
			map.put("score", score);
		}
	    map.put("message", "true");
		return JSONObject.fromObject(map).toString();
	}
}
